import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ValidationPipe } from "@nestjs/common";
import { ResponseInterceptor } from "./common/response.interceotor";
import { HttpExceptionFilter } from "./common/http-exception.filter";
import { serverConfig, dbConfig } from "./common/config";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { WsAdapter } from "@nestjs/platform-ws";
// import { AuthGuard } from './common/auth.guard';

async function bootstrap() {
  // const environment = process.env.NODE_ENV || 'development';
  // const envFilePath = `.env.${environment}`;
  // console.log(envFilePath,"envFilePath")
  const app = await NestFactory.create(AppModule, { cors: true });
  app.setGlobalPrefix("api/");
  // 构建swagger文档
  const options = new DocumentBuilder()
    .setTitle("nestjs_curd")
    .setDescription("Background system based on Nest.js development")
    .setVersion("1.0")
    .setExternalDoc(
      "Swagger配置文档地址",
      "https://blog.csdn.net/qq_36117388/article/details/140517419",
    )
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup("docs", app, document);

  app.useGlobalPipes(new ValidationPipe()); //{ disableErrorMessages: true } // 管道 dto入参认证
  // app.useGlobalGuards(new AuthGuard()); // 全局守卫认证
  app.useGlobalInterceptors(new ResponseInterceptor()); // 全局拦截器 统一响应
  app.useGlobalFilters(new HttpExceptionFilter()); // 全局过滤器 异常
  // websocket
  app.useWebSocketAdapter(new WsAdapter(app));
  await app.listen(serverConfig.PORT, () => {
    console.log(`服务器执行目录：${__dirname}`);
    console.log(`服务启动地址：http://localhost:${serverConfig.PORT}`);
    console.log(`websocket地址：ws://localhost:${serverConfig.PORT}`); //可单独修改ws端口
    console.log(`Swagger地址：http://localhost:${serverConfig.PORT}/docs`);
    console.log(`使用的数据库：${dbConfig.systemDB}`);
  });
}
bootstrap();
